package view.cloudFactory;

import com.jfoenix.controls.JFXCheckBox;
import com.jfoenix.controls.JFXComboBox;
import dao.EquipMsgDao;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;
import pojo.equip.EquipMsgEntity;
import pojo.order.PaiChanEntity;
import pojo.order.PaiChanProperty;
import view.Main;

import java.io.IOException;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Limohan
 */
public class addEquipForBidController {

    private Main myApp ;

    public void setApp(Main myApp) {
        this.myApp = myApp;
    }

    private EquipMsgDao equipMsgDao = new EquipMsgDao();
    private static List<PaiChanEntity> paiChanEntities = new ArrayList<>();
    private static List<PaiChanEntity> AllPaiChanEntities = new ArrayList<>();

    @FXML
    private TableView<PaiChanProperty> orderTable;

    @FXML
    private TableColumn<PaiChanProperty, Void> option;

    @FXML
    private TableColumn<PaiChanProperty, String> equipName;

    @FXML
    private TableColumn<PaiChanProperty, String> startTime;

    @FXML
    private TableColumn<PaiChanProperty, String> endTime;

    @FXML
    private JFXComboBox<String> newEquipName;

    @FXML
    private DatePicker newStartTime;

    @FXML
    private DatePicker newEndTime;


    @FXML
    void initialize() throws IOException {
        for (EquipMsgEntity equipMsgEntity:equipMsgDao.findMsgList()) {
            newEquipName.getItems().add(equipMsgEntity.getName());
        }
    }

    @FXML
    void addNewEquip(ActionEvent event) {
        PaiChanEntity paiChanEntity = new PaiChanEntity();
        paiChanEntity.setEquipName(newEquipName.getValue());
        paiChanEntity.setStartTime(newStartTime.getValue().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        paiChanEntity.setEndTime(newEndTime.getValue().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        AllPaiChanEntities.add(paiChanEntity);
        this.displayInfo(AllPaiChanEntities);

    }

    @FXML
    void deleteEquip(ActionEvent event) {
        AllPaiChanEntities.remove(paiChanEntities.get(0));
        this.displayInfo(AllPaiChanEntities);
    }

    @FXML
    void returnLastPage(ActionEvent event) throws Exception {
        myApp.goToFactoryOrderPage();
    }


    private void addButtonToTable( TableColumn<PaiChanProperty, Void> column){
        Callback<TableColumn<PaiChanProperty, Void>, TableCell<PaiChanProperty, Void>> cellFactory = new Callback<TableColumn<PaiChanProperty, Void>, TableCell<PaiChanProperty, Void>>() {
            @Override
            public TableCell<PaiChanProperty, Void> call(final TableColumn<PaiChanProperty, Void> param) {
                final TableCell<PaiChanProperty, Void> cell = new TableCell<>() {

                    final JFXCheckBox btn = new JFXCheckBox();

                    {
                        btn.setOnAction((ActionEvent event) -> {
                            PaiChanProperty paiChanProperty = getTableView().getItems().get(getIndex());
                            PaiChanEntity paiChanEntity = new PaiChanEntity();
                            paiChanEntity.setEquipName(paiChanProperty.getEquipName());
                            paiChanEntity.setStartTime(paiChanProperty.getStartTime());
                            paiChanEntity.setEndTime(paiChanProperty.getEndTime());
                            try {
                                paiChanEntities.add(paiChanEntity);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        });
                    }

                    @Override
                    public void updateItem(Void item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setGraphic(null);
                        } else {
                            setGraphic(btn);
                        }
                    }
                };
                return cell;
            }
        };

        column.setCellFactory(cellFactory);

    }

    public void displayInfo(List<PaiChanEntity> paiChanEntityList){
        orderTable.getItems();
        ObservableList<PaiChanProperty> paiChanProperties = FXCollections.observableArrayList();
        if(paiChanEntityList!=null){
            for(PaiChanEntity paiChanEntity:paiChanEntityList){
                PaiChanProperty paiChanProperty = new PaiChanProperty();
                paiChanProperty.setEquipName(paiChanEntity.getEquipName());
                paiChanProperty.setStartTime(paiChanEntity.getStartTime());
                paiChanProperty.setEndTime(paiChanEntity.getEndTime());
                paiChanProperties.add(paiChanProperty);
            }

            addButtonToTable(option);

            equipName.setCellValueFactory(param -> param.getValue().equipNameProperty());
            startTime.setCellValueFactory(param -> param.getValue().startTimeProperty());
            endTime.setCellValueFactory(param -> param.getValue().endTimeProperty());
            orderTable.setItems(paiChanProperties);
        }else{
            orderTable.setItems(FXCollections.emptyObservableList());
        }

    }

}
